package com.qifan.alb2b.service.resource;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.qifan.alb2b.dao.CarInfoDao;
import com.qifan.alb2b.dao.CityInfoDao;
import com.qifan.alb2b.domain.CarInfoDomain;
import com.qifan.alb2b.domain.TravelItemInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.cache.AppCache;
import com.qifan.alb2b.service.route.SevenStPrice;
import com.qifan.alb2b.service.route.SixStPrice;
import com.qifan.alb2b.service.vo.Resource;
import com.qifan.alb2b.service.vo.ResourceVo;
import com.qifan.alb2b.service.vo.RouteDetailVo;
import com.qifan.alb2b.service.vo.RouteVo;
import com.qifan.alb2b.service.vo.StPriceTable;
import com.qifan.alb2b.util.Arith;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ExchangeUtil;
import com.qifan.alb2b.util.Page;
import com.qifan.alb2b.util.Stringer;

/**
 * 接送资源
 * @author hc24
 *
 */
@Component
public class CarResource implements IResource {
	private static String[] header=new String[]{"ID","车型","类型","座次","价格/人"};
	
	private CarInfoDao carInfoDao;
	private CityInfoDao cityInfoDao;
	
	public void setCarInfoDao(CarInfoDao carInfoDao) {
		this.carInfoDao = carInfoDao;
	}

	public void setCityInfoDao(CityInfoDao cityInfoDao) {
		this.cityInfoDao = cityInfoDao;
	}


	public String[] getHeader(){
		return header;
	}
	
	public boolean isSingleSelect(){
		return true;
	}
	/**
	 * 车型  	类型   座次   价格/人
	 */
	public ResTable getResTable(String cityId,int groupType,DataGridModel dgm) {
		//根据团队类型判断,最大人数
		int personNum=6;
		if(groupType==Constants.GROUP_TYPE_SIX)
			personNum=SixStPrice.MAX_PERSON_NUM;
		else if(groupType==Constants.GROUP_TYPE_SEVEN)
			personNum=SevenStPrice.MAX_PERSON_NUM;
		
		//根据城市信息查询车辆信息
		CarInfoDomain cid=new CarInfoDomain();
		cid.setCityId(cityId);
		dgm.setSort("price");//设置排序字段
		dgm.setOrder("asc");//设置排序规则
		Page page= carInfoDao.query(dgm, cid);
		List<CarInfoDomain> list=page.getRows();
		
		//获得城市的标准配套价格
		List<Map<String, Object>> mapList=cityInfoDao.queryCityStdInfo(new String[]{cityId});
		if(mapList.size()==0)
			throw new BusinessException("设置接送信息失败，该目的城市不存在");
		Map<String, Object> carStMap=mapList.get(0);
		String countryId=carStMap.get("countryId").toString();
		double carPrice= carStMap.get("carPrice")==null?0.0d:Double.parseDouble(carStMap.get("carPrice").toString());
		carPrice=ExchangeUtil.changeMoney(carPrice, countryId);//汇率转换

		//封装表格每行的数据
		List<Map<String, Object>> datas=new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			CarInfoDomain car=list.get(i);
			Map<String, Object> map=new LinkedHashMap<String, Object>();
			map.put(header[0], car.getCarId());
			if(car.getCarId().equals(carStMap.get("carId").toString())){
				map.put(header[1], "待定");//车型
				map.put(header[2], "散拼");//类型
				map.put(header[3], "待定");//座次
				map.put(header[4], "[+0]起");//价格/人
			}else{
				map.put(header[1], car.getCarType());
				map.put(header[2], "专车");
				map.put(header[3], car.getSeatCount());
				double newCarPrice=ExchangeUtil.changeMoney(car.getPrice(), countryId);
				double diffPrice=Arith.sub(Arith.div(newCarPrice, personNum,Constants.XIAOSHU),Arith.div(carPrice,personNum,Constants.XIAOSHU));				
				String strDiff="[+"+Stringer.formatNumber(diffPrice,"0")+"]起";
				if(diffPrice<0){
					strDiff="["+Stringer.formatNumber(diffPrice,"0")+"]起";
				}
				map.put(header[4], strDiff);
			}
			map.put("查看详情", "<a href='javascript:void(0);' onclick=\"showDetail('"+cityId+"',"+Constants.RES_TYPE_CAR+",'"+car.getCarId()+"')\">查看详情</a>");
			datas.add(map);		
		}
		page.setRows(datas);//重新设置分页的数据
		
		ResTable rt=new ResTable();
		rt.setHeader(header);
		rt.setPage(page);
		rt.setSingleSelect(true);
		return rt;
	}

	/**
	 * 人数   车型   类型   价格/人
	 */
	public ResTable getResDetailTable(String cityId,String resId,int groupType) {
		//判断团队类型
		String[] column=new String[]{};
		int[] persons_num = new int[]{};
		if(groupType==Constants.GROUP_TYPE_SIX){
			column=SixStPrice.COLUMN;
			persons_num=SixStPrice.PERSON_NUMS;
		}else if(groupType==Constants.GROUP_TYPE_SEVEN){
			column=SevenStPrice.COLUMN;
			persons_num=SevenStPrice.PERSON_NUMS;
		}
		
		//获得车辆接送信息
		String[] header=new String[]{"人数","车型","价格"};
		CarInfoDomain car=carInfoDao.getCarById(resId);
		if(car==null)
			throw new BusinessException("该车型不存在，请联系管理员");
		
		//获得城市的标准配套价格
		List<Map<String, Object>> mapList=cityInfoDao.queryCityStdInfo(new String[]{cityId});
		if(mapList.size()==0)
			throw new BusinessException("设置接送信息失败，该目的城市不存在");
		Map<String, Object> carStMap=mapList.get(0);
		String countryId=carStMap.get("countryId").toString();
		double carPrice= carStMap.get("carPrice")==null?0.0d:Double.parseDouble(carStMap.get("carPrice").toString());
		carPrice=ExchangeUtil.changeMoney(carPrice, countryId);//汇率转换
		
		List<Object[]> datas=new ArrayList<Object[]>();
		for(int i=0;i<persons_num.length;i++){
			Object[] obj=new Object[header.length];
			obj[0]=persons_num[i];//车型
			if(car.getCarId().equals(carStMap.get("carId").toString())){
				obj[1]="散拼";
				obj[2]="[+0]起";
			}else{
				obj[1]=car.getCarType();
				double newCarPrice=ExchangeUtil.changeMoney(car.getPrice(), countryId);
				double diffPrice=Arith.sub(Arith.div(newCarPrice, persons_num[i],Constants.XIAOSHU),Arith.div(carPrice,persons_num[i],Constants.XIAOSHU));				
				String strDiff="[+"+Stringer.formatNumber(diffPrice,"0")+"]起";
				if(diffPrice<0){
					strDiff="["+Stringer.formatNumber(diffPrice,"0")+"]起";
				}
				obj[2]=strDiff;
			}
			datas.add(obj);
		}
		Page page=new Page();
		page.setRows(datas);
		
		ResTable rt=new ResTable();
		rt.setHeader(header);
		rt.setPage(page);
		rt.addRemark("描述", car.getRemark());
		rt.setSingleSelect(false);
		return rt;
	}

	public void addOrUpdate(RouteVo routeVo, String routeDetailId, String[] resIds) {
//		if(resIds==null||resIds.length==0)
//			throw new BusinessException("标配资源必须选择");
		if(resIds==null)
			throw new BusinessException("标配资源必须选择");
		//=====查找线路详情=====
		List<RouteDetailVo> detailList= routeVo.getDetailVoList();
		RouteDetailVo routeDetailVo = null;
		for(RouteDetailVo rdv:detailList){
			if(rdv.getRouteDetail().getDetailId().equals(routeDetailId)){
				routeDetailVo=rdv;
				break;
			}
		}
		
		if(routeDetailVo==null)
			return;
		//=====更新接送资源resList=====
		String countryId=routeVo.getRoute().getCountryId();
		CarInfoDomain oldCar=null;
		CarInfoDomain newCar=null;
		List<ResourceVo> resList=routeDetailVo.getResList();
		
		if(resIds==null||resIds.length==0){//①代表清除
			for(int i=0;i<resList.size();i++){
				ResourceVo rv=resList.get(i);
				if(rv.getResType()==Constants.RES_TYPE_CAR){
					oldCar= (CarInfoDomain) rv.getResource();//旧的
					newCar=new CarInfoDomain();//把新对象赋值给一个“0”对象
					newCar.setCarId("");
					newCar.setPrice(0);
					resList.remove(i);//移除
					break;
				}//end if
			}//end for
		}else{//②代表增加或者更新
			//首先把旧的全部移除
			for(int i=0;i<resList.size();i++){
				ResourceVo rv=resList.get(i);
				if(rv.getResType()==Constants.RES_TYPE_CAR){
					oldCar=(CarInfoDomain) rv.getResource();
					resList.remove(i);//移除
					break;
				}//end if
			}//end for
			if(oldCar==null){//如果没有旧对象，则赋值成“0”对象
				oldCar=new CarInfoDomain();
				oldCar.setCarId("");
				oldCar.setPrice(0);
			}
			//其次把新的全部加进来
			newCar=carInfoDao.getCarById(resIds[0]);
			ResourceVo rv=new ResourceVo();
			rv.setIsStd(1);
			rv.setResId(newCar.getCarId());
			newCar.setPrice((float) ExchangeUtil.changeMoney(newCar.getPrice(),countryId));//在这里就把价格转化成人民币!!!
			rv.setResource(newCar);
			rv.setResType(Constants.RES_TYPE_CAR);
			resList.add(rv);
			
		}
		
//		for(int i=0;i<resList.size();i++){
//			ResourceVo rv=resList.get(i);
//			if(rv.getResType()==Constants.RES_TYPE_CAR){
//				if(!rv.getResId().equals(resIds[0])){
//					oldCar=(CarInfoDomain) rv.getResource();
//					rv.setResId(resIds[0]);
//					newCar.setPrice((float) ExchangeUtil.changeMoney(newCar.getPrice(),countryId));//在这里就把价格转化成人民币!!!
//					rv.setResource(newCar);
//					break;
//				}//end if
//			}//end if
//		}//end for
		
//		if(oldCar==null)
//			return;
		//=====更新标配价格表StTable=====
		StPriceTable spt=routeVo.getStPriceTable();
		List<double[]> datas= spt.getDatas();
		String[] rowType=spt.getRowTypes();
		int[] persons_num = null;
		int groupType=routeVo.getGroupType();
		if(groupType==Constants.GROUP_TYPE_SIX){
			persons_num=SixStPrice.PERSON_NUMS;
		}else if(groupType==Constants.GROUP_TYPE_SEVEN){
			persons_num=SevenStPrice.PERSON_NUMS;
		}
		double childScale=AppCache.configInfoMap.get("child_car_scale")==null?
				0.75:Double.parseDouble(AppCache.configInfoMap.get("child_car_scale").getConfigVal());//儿童坐车优惠比例
		
		for(int i=0;i<rowType.length;i++){//每行	
			double[] rows=datas.get(i);
			for(int j=0;j<rows.length;j++){//没列
				double newCarPrice=Arith.div(newCar.getPrice(), persons_num[j],Constants.XIAOSHU);
				double oldCarPrice=Arith.div(oldCar.getPrice(),persons_num[j],Constants.XIAOSHU);
				double newChildCarPrice=Arith.roundCeiling(Arith.mul(newCarPrice, childScale), Constants.XIAOSHU);
				double oldChildCarPrice=Arith.roundCeiling(Arith.mul(oldCarPrice, childScale), Constants.XIAOSHU);
				double diffPrice=Arith.sub(newCarPrice,oldCarPrice);
				double diffChildPrice=Arith.sub(newChildCarPrice, oldChildCarPrice);
				if(rowType[0].contains("儿童")){
					rows[j]=Arith.add(rows[j], diffChildPrice);
				}else{
					rows[j]=Arith.add(rows[j], diffPrice);
				}
			}//end for j			
		}//end for i
	}

	public void calcPrice(TravelItemInfoDomain travelItem, Resource resource,
			int personNum) {
		CarInfoDomain car=(CarInfoDomain) resource;
		double childScale=AppCache.configInfoMap.get("child_car_scale")==null?
				0.75:Double.parseDouble(AppCache.configInfoMap.get("child_car_scale").getConfigVal());//儿童坐车优惠比例
		double price=Arith.div(car.getPrice(), personNum,Constants.XIAOSHU);//根据人数计算平均价格
		double childPrice=Arith.roundCeiling(Arith.mul(price, childScale), Constants.XIAOSHU);//计算儿童价格
		travelItem.setPrice(price);
		travelItem.setRemark("儿童价格："+childPrice);
	}
	
	
}
	
	 


